<template>
  <div class="loginreg">
    <van-nav-bar fixed z-index="11">
      <template #left>
        <i
          class="iconfont icon-fanhui"
          style="font-size: 20px; font-weight: 600"
          @click="$router.back()"
        ></i>
      </template>
      <template #title>
        <span style="font-size: 20px; font-weight: 600">登录/注册</span>
      </template>
    </van-nav-bar>
    <div class="body">
      <h1>登录</h1>
      <label class="input"
        ><input v-model="username" type="text" placeholder="请输入手机号"
      /></label>
      <label class="input pwd"
        ><input v-model="password" type="password" placeholder="密码"
      /></label>
      <div class="button">
        <button @click="onSubmit">登 录</button>
      </div>
    </div>
    <van-popup v-model="show">
      <div class="content">用户名或密码不能为空</div>
    </van-popup>
    <van-popup v-model="showerr">
      <div class="content">密码不对</div>
    </van-popup>
  </div>
</template>

<script>
export default {
  data() {
    return {
      username: "",
      password: "",
      show: false,
      userinfo: [],
      showerr: false,
    };
  },
  created() {
    this.userinfo = JSON.parse(window.localStorage.getItem("userinfo")) || [];
  },
  methods: {
    onSubmit() {
      if (this.username == "" || this.password == "") {
        this.show = true;
      } else {
        if (this.userinfo.length != 0) {
          let k = true;
          this.userinfo.forEach((a) => {
            if (a.username == this.username) {
              if (a.password != this.password) {
                this.showerr = true;
                k = false;
              } else {
                window.localStorage.setItem('logined',1)
                this.$router.push(
                  { path: "/mypage",query:{login:1} },
                  onComplete => {},
                  onAbort => {}
                );
              }
            }
          });
          if (k) {
            this.userinfo.push({
              username: this.username,
              password: this.password,
            });
          }
          window.localStorage.setItem(
            "userinfo",
            JSON.stringify(this.userinfo)
          );
        } else {
          this.userinfo.push({
            username: this.username,
            password: this.password,
          });
          window.localStorage.setItem(
            "userinfo",
            JSON.stringify(this.userinfo)
          );
          this.$router.back()
        }
      }
      window.localStorage.setItem('logined',1)
      this.username = "";
      this.password = "";
    },
  },
};
</script>

<style lang="less" scoped>
.loginreg {
  width: 100vw;
  height: 100vh;
  display: flex;
  justify-content: center;
  padding-top: 70px;
  .content {
    height: 80px;
    //   border-radius: 40px!important;
    color: #888;
    padding: 0 20px;
    display: flex;
    justify-content: center;
    align-items: center;
    text-align: center;
    font-size: 18px;
  }
  .body {
    h1 {
      font-size: 30px;
      font-weight: 600;
      margin-bottom: 60px;
    }
    padding: 0 20px;
    width: 100%;
    .input {
      width: 100%;
      height: 60px;
      display: flex;
      justify-content: center;
      align-items: center;
      border-top: 1px solid #e0e0e0;
      &.pwd {
        border-bottom: 1px solid #e0e0e0;
      }
      input {
        border: none;
        outline: none;
        background-color: #fff;
        height: 50px;
        width: 100%;
        font-size: 16px;
        &::placeholder {
          color: #e0e0e0;
        }
      }
    }
    .button {
      display: flex;
      width: 100%;
      justify-content: space-around;
      margin-top: 30px;
      button {
        width: 98%;
        border: none;
        outline: none;
        height: 46px;
        background-color: #ee0a24;
        color: white;
        font-size: 18px;
        word-spacing: 10px;
        border-radius: 10px;
      }
    }
  }
}
</style>